// Decompiled by DJ v3.11.11.95 Copyright 2009 Atanas Neshkov  Date: 22/03/2011 12:21:29 p.m.
// Home Page: http://members.fortunecity.com/neshkov/dj.html  http://www.neshkov.com/dj.html - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   SelectorDeComprasPendientes.java

package com.luxsoft.sw3.impap.ui.selectores;

import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.UniqueList;
import ca.odell.glazedlists.gui.TableFormat;
import com.jgoodies.uif.builder.ToolBarBuilder;
import com.jgoodies.uifextras.panel.HeaderPanel;
import com.luxsoft.siipap.compras.model.Compra2;
import com.luxsoft.siipap.model.core.Proveedor;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.swing.dialog.AbstractSelector;
import com.luxsoft.siipap.swing.utils.CommandUtils;
import com.luxsoft.siipap.swing.utils.SWExtUIManager;
import com.luxsoft.sw3.services.ComprasManager;
import java.awt.Dimension;
import java.io.PrintStream;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class SelectorDeComprasPendientes extends AbstractSelector
{
	private Proveedor proveedor;

    private SelectorDeComprasPendientes()
    {
        super(Compra2.class, "");
        setTitle("Compras pendientes ");
    }

    protected TableFormat getTableFormat()
    {
        String props[] = {
            "sucursal.nombre", "folio", "fecha", "nombre", "entrega", "consolidada", "importacion", "comentario"
        };
        String labels[] = {
            "Sucursal", "Folio", "Fecha", "Nombre", "Entrega", "Consolidada", "Importaci\363n", "Comentario"
        };
        return GlazedLists.tableFormat(Compra2.class, props, labels);
    }

    public void initGlazedLists()
    {
        source = (EventList)GlazedLists.threadSafeList(new BasicEventList());
        source = new UniqueList(source, GlazedLists.beanPropertyComparator(Compra2.class, "id"));
        sortedSource = new SortedList(buildFilterList(), getComparator());
    }

    protected JComponent buildHeader()
    {
        header = new HeaderPanel("Compras pendientes de entrega", "");
        return header;
    }

    protected JComponent buildToolbar()
    {
        JToolBar bar = new JToolBar();
        ToolBarBuilder builder = new ToolBarBuilder(bar);
        Action a = CommandUtils.createViewAction(this, "cambiarPeriodo");
        a.putValue("Name", "Buscar");
        a.putValue("ShortDescription", "Buscar facturas en otro periodo");
        builder.add(a);
        builder.add(buildFilterPanel());
        return builder.getToolBar();
    }

    protected void onWindowOpened()
    {
        load();
    }

    protected void setPreferedDimension(JComponent gridComponent)
    {
        gridComponent.setPreferredSize(new Dimension(750, 450));
    }

    protected java.util.List getData()
    {
    	if(getProveedor()!=null){
    		String hql = "select cu.compra from CompraUnitaria cu  " +
    				" where cu.compra.proveedor.id=? " +
    				" and cu.solicitado-cu.recibido>0 and cu.depuracion is null";
            return ServiceLocator2.getHibernateTemplate().find(hql,getProveedor().getId());
    	}else{
    		String hql = "select cu.compra from CompraUnitaria cu  where cu.solicitado-cu.recibido>0 and cu.depuracion is null";
            return ServiceLocator2.getHibernateTemplate().find(hql);
    	}
        
    }

    public void clean()
    {
        source.clear();
    }

    protected TextFilterator getBasicTextFilter()
    {
        return GlazedLists.textFilterator(new String[] {
            "folio", "nombre"
        });
    }
    
    

    public Proveedor getProveedor() {
		return proveedor;
	}

	public void setProveedor(Proveedor proveedor) {
		this.proveedor = proveedor;
	}

	public static Compra2 seleccionar()
    {
        SelectorDeComprasPendientes selector = new SelectorDeComprasPendientes();
        selector.setSelectionMode(0);
        selector.open();
        if(!selector.hasBeenCanceled())
        {
            Compra2 selected = (Compra2)selector.getSelected();
            selector.clean();
            return ServiceLocator2.getComprasManager().buscarInicializada(selected.getId());
        } else
        {
            return null;
        }
    }
    
    public static Compra2 seleccionar(Proveedor p)
    {
        SelectorDeComprasPendientes selector = new SelectorDeComprasPendientes();
        selector.setProveedor(p);
        selector.setSelectionMode(0);
        selector.open();
        if(!selector.hasBeenCanceled())
        {
            Compra2 selected = (Compra2)selector.getSelected();
            selector.clean();
            return ServiceLocator2.getComprasManager().buscarInicializada(selected.getId());
        } else
        {
            return null;
        }
    }

    public static void main(String args[])
    {
        SWExtUIManager.setup();
        SwingUtilities.invokeLater(new Runnable() {

            public void run()
            {
                Compra2 res = SelectorDeComprasPendientes.seleccionar();
                System.out.println(res);
                System.exit(0);
            }

        }
);
    }

    private HeaderPanel header;
}
